package com.hxzy.system.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hxzy.common.constant.UserConstants;
import com.hxzy.common.core.domain.entity.SysDictType;
import com.hxzy.common.utils.DictUtils;
import com.hxzy.common.utils.StringUtils;
import com.hxzy.system.domain.dto.DictTypeSearchDTO;
import com.hxzy.system.mapper.SysDictDataMapper;
import com.hxzy.system.mapper.SysDictTypeMapper;
import com.hxzy.system.service.ISysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * 字典业务层
 * @author 64689
 */
@Service
@Transactional
public class SysDictTypeServiceImpl extends ServiceImpl<SysDictTypeMapper, SysDictType> implements ISysDictTypeService {
    @Autowired
    private SysDictTypeMapper dictTypeMapper;

    @Autowired
    private SysDictDataMapper dictDataMapper;



    /**
     * 根据条件分页查询字典数据
     *
     * @param dictDataDto 字典数据信息
     * @return 字典数据集合信息
     */

    @Override
    public IPage<SysDictType> search(DictTypeSearchDTO dictDataDto) {
        Page<SysDictType> page = PageDTO.of(dictDataDto.getPageNum(), dictDataDto.getPageSize());
        return super.baseMapper.search(page,dictDataDto);
    }
    /**
     * 校验字典类型称是否唯一
     *
     * @param dict 字典类型
     * @return 结果
     */
    @Override
    public String checkDictTypeUnique(SysDictType dict) {
        //根据前端传过来的字典对象获取字典Id
        long dicId = StringUtils.isNull(dict.getDictId()) ? -1L : dict.getDictId();
        //根据前端对象字典类型查出数据库对象
        SysDictType dictType = this.dictTypeMapper.checkDictTypeUnique(dict.getDictType());
       if(StringUtils.isNotNull(dictType)&&dictType.getDictId()!=dicId){
           return  UserConstants.NOT_UNIQUE;
       }
        return UserConstants.UNIQUE;
    }

    @Override
    public int insertDicType(SysDictType dict) {
        return this.baseMapper.insertDicType(dict);
    }
    /**
     * 修改保存字典类型信息
     *
     * @param dict 字典类型信息
     * @return 结果
     */
    @Override
    public boolean updateBy(SysDictType dict) {


        return this.baseMapper.updateBy(dict);
    }



    /**
     * 根据字典类型ID查询信息
     *
     * @param dictId 字典类型ID
     * @return 字典类型
     */
    @Override
    public SysDictType selectDictTypeById(Long dictId) {
        return this.baseMapper.selectDictTypeById(dictId);
    }

    @Override
    public int deleteBy(Long[] dictId) {
        for (Long  id: dictId) {
            SysDictType sysDictType = this.baseMapper.selectDictTypeById(id);
            if(Objects.isNull(sysDictType)){
                throw new RuntimeException("已删除");
            }

        }
        int i=  this.baseMapper.deleteBy(dictId);
        if(i>0){
            DictUtils.clearDictCache();
        }
        return i;
    }

    /**
     * 根据所有字典类型
     *
     * @return 字典类型集合信息
     */
    @Override
    public List<SysDictType> selectDictTypeAll() {
        return this.baseMapper.selectDictTypeAll();
    }
    /**
     * 清空缓存数据
     */
    @Override
    public void clearCache() {
        DictUtils.clearDictCache();
    }





}
